// 前端

axios.defaults.baseURL = 'http://124.223.14.236:8060/home'


// 添加请求拦截器
axios.interceptors.request.use(config => {
  // Do something before request is sent
  const token = localStorage.getItem('token')
  if (token) {
    config.headers.Authorization = token
    config.headers.token = token
  }
  return config
}, error => {
  // Do something with request error
  return Promise.reject(error)
})


// 头部
const renderList = async () => {
  const { data: res } = await axios.get(`/index/index`)

  // console.log(res)
  $('.nav').append(res.data.allCate.map(item => {
    return `
    <li class="menu-item menu-item-type-taxonomy menu-item-object-category">
    <a href="list.html?cateid=${item.id}&catename=${item.catename}"><span
    class="fa ${item.icon}"></span>${item.catename}</a></li>
    `
  }).join(''))

  $('.nav li').click(function () {
    $(this).addClass('current_page_item').siblings().removeClass('current_page_item')
  })

}
renderList()




const render = async () => {
  const { data: res } = await axios.get('/index/index')
  // console.log(res);

  // 右侧1 sideBanner
  $('.textwidget').html(res.data.sideBanner.map(item => {
    return `
    <a href="#/" rel="nofollow" target="_blank" title="${item.advimgdesc}">
            <img class="g430" style="width:360px;height:133px;" src="http://124.223.14.236:8060/${item.advimgsrc}" alt="${item.advimgdesc}">
          </a>
    `
  }).join(''))

  // 右侧3 allTag热门标签
  $('.widget_tags_inner').html(res.data.allTag.map(item => {
    return `<li><a title="" href="#/">${item.tagname}</a></li>`
  }).join(''))

  // 右侧 通知公告
  $('.widget_textssr').html(res.data.config.allData.map(item => {
    if (item.attr_key === 'notic_content') {
      // console.log(item)
      return `
      <a class="style02" href="http://www.thefox.cn/alibaixiu.shtml"
          target="_blank"><strong>通知公告</strong>
          <h2>感谢广大好友</h2>
          <p> ${item.attr_value}</p>
        </a>
      `
    }
  }).join(''))


  // 右侧5 友情链接
  $('.widget_comments ul').html(res.data.allLink.map(item => {
    return `
          <li>
            <a href="#/"><img src="http://124.223.14.236:8060/${item.img}" alt="">
              <p>${item.des}</p>
            </a>
          </li>
    `
  }).join(''))

  // 底部 友情链接
  // $('.link span').html(res.data.allLink.map((item, index) => {
  //   `
  //     <a href="${item.url}" title="${item.title}">${item.title}</a> 
  //     <span>|</span>
  //     `

  // }).join(''))

  $('.footer p').html(res.data.config.allData.map(item => {
    if (item.attr_key === 'copy_info') {
      return `
      &copy; 2021 
      <a href="http://blog.weknow.cn">${item.attr_value}</a>
      &nbsp;&nbsp;联系邮箱：hewu1122@163.com
      &nbsp;&nbsp; <a href="#/">皖-备88888888</a>
      `
    }
  }).join(''))

}
render()


// 右侧2 热门文章
const recommend = async () => {
  const { data: res } = await axios.get('/index/recommend')
  // console.log(res)

  $('.items-02').html(res.data.list.map(item => {
    return `
    <li>
    <a target="_blank" href="">
      <span class="thumbnail">
        <img src="http://124.223.14.236:8060/${item.pic}" class="thumb" />
      </span>
      <span class="text">${item.title}</span>
      <span class="text-muted post-views">阅读(${item.click})</span>
    </a>
  </li>
    `
  }).join(''))


  // 右侧4 最新推荐
  $('.items-01').html(res.data.list.map(item => {
    return `
          <li>
            <a target="_blank" href="">
              <span class="thumbnail">
                <img class="thumb" src="http://124.223.14.236:8060/${item.pic}" />
              </span>
              <span class="text">${item.title}</span>
              <span class="text-muted post-views">阅读(${item.click})</span>
              </a></span>
            </a>
          </li>
    `
  }))
}
recommend()


// 定义时间规范
// 格式化时间


function formatTime(date) {
  const dt = new Date(date)

  var y = dt.getFullYear()
  var m = padZero(dt.getMonth() + 1)
  var d = padZero(dt.getDate())

  var hh = padZero(dt.getHours())
  var mm = padZero(dt.getMinutes())
  var ss = padZero(dt.getSeconds())

  return y + '-' + m + '-' + d + ' ' + hh + ':' + mm + ':' + ss
}

// 定义补零的函数
function padZero(n) {
  return n > 9 ? n : '0' + n
}


//登录 注册 退出
const token = localStorage.getItem('token')
const username = localStorage.getItem('username')
$('.username').html(username)
if (token) {
  $('.slinks').hide()
  $('.logined').show()
} else {
  $('.slinks').show()
  $('.logined').hide()
}

$('.fa-sign-out').on('click', function () {
  localStorage.removeItem('token')
  localStorage.removeItem('username')
  location.href = '/web-static/index.html'
})


